package com.threeox.commonlibrary.ui.fragment.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.threeox.commonlibrary.entity.engine.Lifecycle;
import com.threeox.commonlibrary.ui.view.engineview.base.CommonModelView;
import com.threeox.commonlibrary.ui.view.inter.engine.base.IBaseExtend;

/**
 * 
 * @ClassName: BaseModelFragment
 * 
 * @Description: TODO()
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/8/10 10:19
 * 
 * @version 1.0
 * 
 */
public abstract class BaseModelFragment extends CommonFragment {

    protected IBaseExtend mBaseExtend;
    protected CommonModelView mCommonModelView;

    protected void initBaseModelMessage(CommonModelView viewGroup) {
        this.mCommonModelView = viewGroup;
        this.mBaseExtend = viewGroup.getBaseExtend();
        if (null != mBaseExtend) {
            mBaseExtend.initActivity(mContext);
            mBaseExtend.initFragment(this);
        }
    }

    /*============================生命周期===========================*/
    @Override
    public void onStart() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onStart);
        }
        if (result) {
            super.onStart();
        }
    }


    @Override
    public void onPause() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onPause);
        }
        if (result) {
            super.onPause();
        }
    }

    @Override
    public void onStop() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onStop);
        }
        if (result) {
            super.onStop();
        }
    }

    @Override
    public void onResume() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onResume);
        }
        if (result) {
            super.onResume();
        }
    }

    @Override
    public void onDestroy() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onDestroy);
        }
        if (result) {
            super.onDestroy();
        }
    }

    /*============================生命周期===========================*/

    public <T extends IBaseExtend> T getBaseExtend() {
        return (T) mBaseExtend;
    }

    public <T extends CommonModelView> T getCommonModelView() {
        return (T) mCommonModelView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (null != mBaseExtend) {
            mBaseExtend.onActivityCreated(savedInstanceState);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null != mCommonModelView) {
            mCommonModelView.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (null != mCommonModelView) {
            mCommonModelView.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    public boolean onBackPressed() {
        if (null != mBaseExtend) {
            return mBaseExtend.onBackPressed();
        }
        return true;
    }

}
